import * as types from '../mutation-types';
import { fetch } from 'utils/api.js';

const state = {
  loading: false,
  specialColumnId:"",
  coursePacketId:"",
  contentList: [],
  totalCount:0
};

const mutations = {
  [types.SET_CONTENT_TOTAL_COUNT](state, totalCount) {
    state.totalCount = totalCount;
  },
  [types.SET_CONTENT_LIST](state, contentList) {
    state.contentList = contentList;
  },
  [types.SET_SPECIALCOLUMNID](state, specialColumnId) {
    state.specialColumnId = specialColumnId;
  },
  [types.SET_COURSEPACKETID](state, coursePacketId) {
    state.coursePacketId = coursePacketId;
  }
};

const actions = {
  loadContentList({ commit, state },{specialColumnId,coursePacketId,page,size}) {
    fetch('contentList',{ body: { id: specialColumnId,packateId:coursePacketId,page:page,size:size } }).then((res) => {
      if (res.code === 200) {
        commit(types.SET_CONTENT_LIST, res.data.dataList);
        commit(types.SET_CONTENT_TOTAL_COUNT, res.data.count);
      } else {
        throw new Error('Response Error');
      }
    });
  },
  setSpecialColumnId({ commit, state },specialColumnId) {
    commit(types.SET_SPECIALCOLUMNID, specialColumnId);
  },
  setCoursePacketId({ commit, state },coursePacketId) {
    commit(types.SET_COURSEPACKETID, coursePacketId);
  }
};

export default {
  state,
  mutations,
  actions
};
